/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/cat.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../data_struct/head.h"
#include "../macro/head.h"
#include "../impl/head.h"
#include "../msg/head.h"
#include "jsp_cmd.h"
#include "helper_jsp.h"

namespace jsp2cpp
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    jsp_cmd::jsp_cmd( helper_jsp * pJsp ) : m_pJsp( pJsp )
    {
        m_pBigBuf = mgr::container()->box()->big_buf();
    }

    jsp_cmd::~jsp_cmd()
    {
        xos_stl::release_interface( m_pBigBuf );
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int jsp_cmd::load_file( xos_box::i_big_buf * pBigBuf, const char * lpszFile )
    {
        int ret = 0;

        xos::i_file * pFile = 0;

        if( 0 == ret )
        {
            pFile = mgr::container()->xos()->file();
        }

        if( 0 == ret )
        {
            ret = pFile->open( lpszFile, xos::i_file::XOS_FILE_READ, xos::i_file::XOS_SHARE_READ, xos::i_file::XOS_FILE_OPEN );
        }

        if( 0 == ret )
        {
            int len = pFile->read( pBigBuf );
            if( len > 0 )
            {
                pBigBuf->set_end_zero();
            }
        }

        xos_stl::release_interface( pFile );

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // parse

    // 
    // <%@ include file="head.jsp" %>
    // 
    int jsp_cmd::parse( xos_box::i_big_buf * pBigBuf, xos_box::i_buf * pBuf )
    {
        int result = 0;
        int ret = 0;

        xos_common::i_misc * pMisc = mgr::container()->common_misc();
        xos::i_crt * pCrt = mgr::container()->crt();

        const char * lpszBuf = pBuf->get_data( 0, 0, 0, 0 );
        const char * lpszBegin = 0;
        const char * lpszEnd = 0;
        int nLen = pBuf->get_len( 0 );

        char * lpszContent = 0;
        char * lpszParam = 0;
        char * lpszCmd = 0;

        char buf[4096] = {0};

        if( ( 0 == ret ) && ( 0 != pMisc->begin_with( lpszBuf, "<%@" ) ) )
        {
            ret = 1;
        }

        if( ( 0 == ret ) && ( 0 != pMisc->end_with( lpszBuf, "%>" ) ) )
        {
            ret = 1;
        }

        if( 0 == ret )
        {
            lpszEnd = lpszBuf + nLen - 2;
            lpszBegin = lpszBuf + 3;
        }

        if( 0 == ret )
        {
            int len = ( int )( lpszEnd - lpszBegin );
            buf[0] = 0;
            pCrt->strncpy( buf, sizeof( buf ), lpszBegin, len );
            pMisc->trim( buf, " " );
        }

        // <%@ include file="head.jsp" %>
        // include file="head.jsp"
        if( 0 == ret )
        {
            lpszContent = pCrt->strstr( buf, "=", true );
            lpszParam = pCrt->strstr( buf, " ", true );
            lpszCmd = buf;
            if( !lpszContent || !lpszParam )
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            *lpszContent++ = 0;
            *lpszParam++ = 0;
            pMisc->trim( lpszContent, "\"" );
        }

        // 现在只支持include一条指令
        if( ( 0 == ret ) && ( 0 != pCrt->strcmp( lpszCmd, "include" ) ) )
        {
            ret = 1;
        }

        if( 0 == ret )
        {
            m_pBigBuf->reset();
            ret = load_file( m_pBigBuf, lpszContent);
        }

        if( 0 == ret )
        {
            m_pBigBuf->add_data( pBigBuf, true );
            pBigBuf->reset();
            pBigBuf->add_data( m_pBigBuf, true );
            pBigBuf->set_end_zero();
        }

        if( 0 == ret )
        {
            result = 1;
        }

        return result;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

} // jsp2cpp
